#include "objectactions.h"

#include "rosterelements.h"

#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#include <QDebug>

ObjectAction::~ObjectAction() {

}

ObjectAction* ObjectAction::createAction(ActionType type) {
    switch(type) {
    case ChangeGlobalTexts:
        return new GlobalTextAction();
    case AddItems:
        return new AddItemAction();
    case AddUnit:
        return new AddUnitAction();
    case ModifyCell:
        return new ModifyCellAction();
    default:
        qDebug() << "ObjectAction::createAction found an unknown type:" << type;
        return NULL;
    }
}

// GlobalTextAction

// \TODO: GlobalTextAction not done

GlobalTextAction::GlobalTextAction() {

}

void GlobalTextAction::doAction(RosterUnit *unit) {

}

void GlobalTextAction::undoAction(RosterUnit *unit) {

}

ObjectAction::ActionType GlobalTextAction::type() const {
    return ObjectAction::ChangeGlobalTexts;
}

void GlobalTextAction::readFromFile(QXmlStreamReader *reader) {

}

void GlobalTextAction::writeToFile(QXmlStreamWriter *writer) {

}

// AddItemAction

AddItemAction::AddItemAction() {

}

AddItemAction::AddItemAction(const QStringList &items) :
    _items(items) {

}

void AddItemAction::doAction(RosterUnit *unit) {
    for(int i = 0; i<_items.size(); ++i)
        unit->addItem(_items.at(i));
}

void AddItemAction::undoAction(RosterUnit *unit) {
    for(int i = _items.size()-1; i>=0; --i)
        unit->subtractItem(_items.at(i));
}

ObjectAction::ActionType AddItemAction::type() const {
    return ObjectAction::AddItems;
}

void AddItemAction::readFromFile(QXmlStreamReader *reader) {
    while(reader->readNextStartElement()) {
        if(reader->name() == "item") {
            QString key = reader->readElementText();
            if(!key.isEmpty())
                _items.append(key);
        } else
            reader->skipCurrentElement();
    }
}

void AddItemAction::writeToFile(QXmlStreamWriter *writer) {
    for(int i = 0; i<_items.size(); ++i) {
        writer->writeTextElement("item", _items.at(i));
    }
}

QStringList AddItemAction::getItems() const {
    return _items;
}

// AddUnitAction

AddUnitAction::AddUnitAction() {

}

AddUnitAction::AddUnitAction(const QString &unitKey, bool replacesModel) :
    _unitKey(unitKey), _replacesModel(replacesModel) {

}

void AddUnitAction::doAction(RosterUnit *unit) {
    unit->addLinkedChild(_unitKey,_replacesModel);
}

void AddUnitAction::undoAction(RosterUnit *unit) {
    unit->removeLinkedChild(_unitKey);
}

ObjectAction::ActionType AddUnitAction::type() const {
    return ObjectAction::AddUnit;
}

void AddUnitAction::readFromFile(QXmlStreamReader *reader) {
    while(reader->readNextStartElement()) {
        if(reader->name() == "unit_key") {
            _unitKey = reader->readElementText();
        } else if(reader->name() == "replaces_model") {
            _replacesModel = reader->readElementText() == "yes" ? true : false;
        } else
            reader->skipCurrentElement();
    }
}

void AddUnitAction::writeToFile(QXmlStreamWriter *writer) {
    writer->writeTextElement("unit_key", _unitKey);
    writer->writeTextElement("replaces_model", _replacesModel ? "yes" : "no");
}

QString AddUnitAction::getUnitKey() const {
    return _unitKey;
}

bool AddUnitAction::replacesModel() const {
    return _replacesModel;
}


// ModifyCellAction

ModifyCellAction::ModifyCellAction() {

}

ModifyCellAction::ModifyCellAction(const QString& cell, const QString& value, bool onlyAppend) :
    _cell(cell), _value(value), _onlyAppend(onlyAppend)
{

}

void ModifyCellAction::doAction(RosterUnit *unit) {
    unit->amountCell(_cell, _value, _onlyAppend);
}

void ModifyCellAction::undoAction(RosterUnit *unit) {
    unit->subtractCell(_cell, _value, _onlyAppend);
}

ObjectAction::ActionType ModifyCellAction::type() const {
    return ObjectAction::ModifyCell;
}

void ModifyCellAction::readFromFile(QXmlStreamReader *reader) {
    while(reader->readNextStartElement()) {
        if(reader->name() == "modify_cell") {
            _cell = reader->attributes().value("key").toString();
            _onlyAppend = reader->attributes().value("append").toString() == "1";
            _value = reader->readElementText();
        } else
            reader->skipCurrentElement();
    }
}

void ModifyCellAction::writeToFile(QXmlStreamWriter *writer) {
    writer->writeStartElement("modify_cell");
    writer->writeAttribute("key", _cell);
    writer->writeAttribute("append", _onlyAppend ? "1" : "0");
    writer->writeCharacters(_value);
    writer->writeEndElement();
}

QString ModifyCellAction::getCellKey() const {
    return _cell;
}

QString ModifyCellAction::getValue() const {
    return _value;
}

bool ModifyCellAction::getOnlyAppend() const {
    return _onlyAppend;
}
